#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2024 Fewtarius

DATA_DIR="${HOME}/.updates"
PACKAGE="${1}"
if [ -z "${2}" ]
then
	DATA_SOURCE="git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git"
else
	DATA_SOURCE="${2}"
fi
WORK_DIR="${HOME}/.update_tmp/${PACKAGE}"

if [ ! -d "${WORK_DIR}" ]
then
	mkdir -p ${WORK_DIR}
	cd ${WORK_DIR}
	git clone ${DATA_SOURCE} >/dev/null 2>&1
else
	cd ${WORK_DIR}/${PACKAGE}
	git fetch >/dev/null 2>&1
fi

cd ${WORK_DIR}/${PACKAGE}
UPSTREAM_VERSION="$(git tag | sort -V | grep -v 'rc' | tail -n 1)"

if [ ! -d "${DATA_DIR}" ]
then
	mkdir -p ${DATA_DIR}
fi

if [ -f "${DATA_DIR}/${PACKAGE}" ]
then
        DOWNSTREAM_VERSION="$(cat ${DATA_DIR}/${PACKAGE})"
fi

if [ ! "${UPSTREAM_VERSION}" = "${DOWNSTREAM_VERSION}" ]
then
	echo "${UPSTREAM_VERSION}" | tee ${DATA_DIR}/${PACKAGE}
fi
